package com.woniu40.petai_manager.web;

import com.github.pagehelper.PageInfo;
import com.woniu40.petai_manager.entity.Hospital;
import com.woniu40.petai_manager.result.ResponseResult;
import com.woniu40.petai_manager.service.HospitalService;
import com.woniu40.petai_manager.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;

@CrossOrigin
@RestController
@Api(value = "医院管理")
@RequestMapping("hospital")
public class HospitalController {

    @Autowired
    HospitalService hospitalService;

    @Value("${filepath}")
    private String uploadPath;

    @PostMapping("/upload")
    public ResponseResult<String> upload(@RequestParam("cover") MultipartFile multipartFile) {
        ResponseResult<String> result=new ResponseResult<>();
        //获得上传图片的名字
        String filename = multipartFile.getOriginalFilename();
        //获取文件后缀
        int index = filename.lastIndexOf(".");
        String subFix = filename.substring(index);  // .jpg
        //获取UUID 来替换文件的名字
        String uuid = UUID.randomUUID().toString();
        //新的文件名
        filename ="lzz_"+ uuid + subFix ;
        //获取上传地址
        try {
            multipartFile.transferTo(new File(uploadPath+File.separator+filename));
            result.setCode(200);
            result.setData(filename);
            result.setMessage("上传成功");
        } catch (IOException e) {
            e.printStackTrace();
            result.setCode(500);
            result.setMessage("上传失败");
        }

        return result;

    }

    @GetMapping("/list")
    @ApiOperation("查询医院列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "hospitalName", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", required = false, dataType = "Integer", paramType = "query")
    })
    public ResponseResult<PageInfo> findAll(String hospitalName,
                                            @RequestParam(defaultValue = "1") Integer pageNum,
                                            @RequestParam(defaultValue = "3") Integer pageSize) {
        PageInfo<Hospital> pageInfo = hospitalService.findHospitalAll(hospitalName, pageNum, pageSize);
        ResponseResult<PageInfo> result = new ResponseResult<>();
        if (pageInfo != null) {
            result.setCode(200);
            result.setData(pageInfo);
            result.setMessage("查询成功");
        } else {
            result.setCode(107);
            result.setMessage("查询失败");
        }
        return result;
    }

    @GetMapping("/toModify")
    @ApiOperation(value = "查询一条医院信息")
    public ResponseResult<Hospital> findById(Integer hospitalId) {
        Hospital hospital = hospitalService.findHosById(hospitalId);

        ResponseResult<Hospital> result = new ResponseResult<>();
        if (hospital != null) {
            result.setCode(200);
            result.setMessage("添加成功");
            result.setData(hospital);
        } else {
            result.setCode(107);
            result.setMessage("添加失败");
        }

        return result;
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加一条医院信息",notes="返回1表示成功")
    public ResponseResult<Hospital> add(@RequestBody Hospital hospital) {
        int i = hospitalService.addHospital(hospital);

        ResponseResult<Hospital> result = new ResponseResult<>();
        if (i == 1) {
            result.setCode(200);
            result.setMessage("添加成功");
            result.setData(hospital);
        } else {
            result.setCode(110);
            result.setMessage("添加失败");
        }

        return result;
    }

    @PutMapping("/modify")
    @ApiOperation(value = "更新一条医院信息",notes="返回1表示成功")
    public ResponseResult<Hospital> modify(@RequestBody Hospital hospital) {
        int i = hospitalService.modifyHospital(hospital);

        ResponseResult<Hospital> result = new ResponseResult<>();
        if (i == 1) {
            result.setCode(200);
            result.setMessage("更新成功");
            result.setData(hospital);
        } else {
            result.setCode(108);
            result.setMessage("更新失败");
        }

        return result;
    }

    @DeleteMapping("/remove/{hospitalId}")
    @ApiOperation(value = "删除一条医院信息",notes="返回1表示成功")
    @ApiImplicitParam(paramType="query",name="hospitalId",value="医院id",required=true,dataType="Integer")
    public ResponseResult remove(@PathVariable(value = "hospitalId") Integer hospitalId) {
        int i = hospitalService.removeHospital(hospitalId);
        ResponseResult<Object> result = new ResponseResult<>();
        if (i == 1) {
            result.setCode(200);
            result.setMessage("删除成功");
        } else {
            result.setCode(109);
            result.setMessage("删除失败");
        }
        return result;
    }

}
